package com.university.evaluation.controller;

import com.university.evaluation.common.ResponseResult;
import com.university.evaluation.domain.dto.EvaluationDTO;
import com.university.evaluation.service.EvaluationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/evaluation")
@Tag(name = "测评管理", description = "提供测评相关的API接口")
public class EvaluationController {

    @Autowired
    private EvaluationService evaluationService;

    @Operation(summary = "测试认证", description = "测试用户是否已认证")
    @GetMapping("/test-auth")
    public ResponseResult<Void> testAuth() {
        try {
            log.info("测试认证请求");
            ResponseResult<Void> result = evaluationService.testAuth();
            
            if (result.isSuccess()) {
                log.info("测试认证成功");
            } else {
                log.warn("测试认证失败，原因: {}", result.getMsg());
            }
            
            return result;
        } catch (Exception e) {
            log.error("测试认证时发生异常: {}", e.getMessage(), e);
            return ResponseResult.failure("测试认证失败: " + e.getMessage());
        }
    }

    @Operation(
        summary = "创建测评", 
        description = "创建新的测评项目"
    )
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "创建成功",
                    content = @Content(schema = @Schema(implementation = ResponseResult.class))),
        @ApiResponse(responseCode = "400", description = "创建参数错误"),
        @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    @PostMapping
    @PreAuthorize("hasRole('ADMIN') or hasRole('TEACHER')")
    public ResponseResult<Map<String, Object>> createEvaluation(
            @Parameter(name = "evaluationDTO", description = "测评信息DTO对象", required = true)
            @RequestBody EvaluationDTO evaluationDTO) {
        try {
            log.info("创建测评请求，测评评论: {}", evaluationDTO.getComments());
            ResponseResult<Map<String, Object>> result = evaluationService.createEvaluation(evaluationDTO);
            
            if (result.isSuccess()) {
                log.info("创建测评成功，测评评论: {}", evaluationDTO.getComments());
            } else {
                log.warn("创建测评失败，测评评论: {}, 原因: {}", evaluationDTO.getComments(), result.getMsg());
            }
            
            return result;
        } catch (Exception e) {
            log.error("创建测评时发生异常: {}", e.getMessage(), e);
            return ResponseResult.failure("创建测评失败: " + e.getMessage());
        }
    }

    @Operation(
        summary = "获取测评列表", 
        description = "查询系统中的测评列表"
    )
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "获取成功",
                    content = @Content(schema = @Schema(implementation = ResponseResult.class))),
        @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    @GetMapping
    public ResponseResult<Map<String, Object>> getEvaluationList() {
        try {
            log.info("获取测评列表请求");
            ResponseResult<Map<String, Object>> result = evaluationService.getEvaluationList();
            
            if (result.isSuccess()) {
                log.info("获取测评列表成功");
            } else {
                log.warn("获取测评列表失败，原因: {}", result.getMsg());
            }
            
            return result;
        } catch (Exception e) {
            log.error("获取测评列表时发生异常: {}", e.getMessage(), e);
            return ResponseResult.failure("获取测评列表失败: " + e.getMessage());
        }
    }

    @Operation(
        summary = "获取测评详情", 
        description = "查询指定ID的测评详情"
    )
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "获取成功",
                    content = @Content(schema = @Schema(implementation = ResponseResult.class))),
        @ApiResponse(responseCode = "404", description = "测评不存在"),
        @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    @GetMapping("/{evaluationId}")
    public ResponseResult<Map<String, Object>> getEvaluationById(
            @Parameter(name = "evaluationId", description = "测评ID", required = true)
            @PathVariable Long evaluationId) {
        try {
            log.info("获取测评详情请求，测评ID: {}", evaluationId);
            ResponseResult<Map<String, Object>> result = evaluationService.getEvaluationById(evaluationId);
            
            if (result.isSuccess()) {
                log.info("获取测评详情成功，测评ID: {}", evaluationId);
            } else {
                log.warn("获取测评详情失败，测评ID: {}, 原因: {}", evaluationId, result.getMsg());
            }
            
            return result;
        } catch (Exception e) {
            log.error("获取测评详情时发生异常: {}", e.getMessage(), e);
            return ResponseResult.failure("获取测评详情失败: " + e.getMessage());
        }
    }

    @Operation(
        summary = "更新测评信息", 
        description = "更新指定ID的测评信息"
    )
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "更新成功",
                    content = @Content(schema = @Schema(implementation = ResponseResult.class))),
        @ApiResponse(responseCode = "404", description = "测评不存在"),
        @ApiResponse(responseCode = "400", description = "更新参数错误"),
        @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    @PutMapping("/{evaluationId}")
    @PreAuthorize("hasRole('ADMIN') or hasRole('TEACHER')")
    public ResponseResult<Void> updateEvaluation(
            @Parameter(name = "evaluationId", description = "测评ID", required = true)
            @PathVariable Long evaluationId,
            @Parameter(name = "evaluationDTO", description = "测评信息DTO对象", required = true)
            @RequestBody EvaluationDTO evaluationDTO) {
        try {
            log.info("更新测评信息请求，测评ID: {}, 测评评论: {}", evaluationId, evaluationDTO.getComments());
            ResponseResult<Void> result = evaluationService.updateEvaluation(evaluationId, evaluationDTO);
            
            if (result.isSuccess()) {
                log.info("更新测评信息成功，测评ID: {}", evaluationId);
            } else {
                log.warn("更新测评信息失败，测评ID: {}, 原因: {}", evaluationId, result.getMsg());
            }
            
            return result;
        } catch (Exception e) {
            log.error("更新测评信息时发生异常: {}", e.getMessage(), e);
            return ResponseResult.failure("更新测评失败: " + e.getMessage());
        }
    }

    @Operation(
        summary = "删除测评", 
        description = "删除指定ID的测评"
    )
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "删除成功",
                    content = @Content(schema = @Schema(implementation = ResponseResult.class))),
        @ApiResponse(responseCode = "404", description = "测评不存在"),
        @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    @DeleteMapping("/{evaluationId}")
    @PreAuthorize("hasRole('ADMIN') or hasRole('TEACHER')")
    public ResponseResult<Void> deleteEvaluation(
            @Parameter(name = "evaluationId", description = "测评ID", required = true)
            @PathVariable Long evaluationId) {
        try {
            log.info("删除测评请求，测评ID: {}", evaluationId);
            ResponseResult<Void> result = evaluationService.deleteEvaluation(evaluationId);
            
            if (result.isSuccess()) {
                log.info("删除测评成功，测评ID: {}", evaluationId);
            } else {
                log.warn("删除测评失败，测评ID: {}, 原因: {}", evaluationId, result.getMsg());
            }
            
            return result;
        } catch (Exception e) {
            log.error("删除测评时发生异常: {}", e.getMessage(), e);
            return ResponseResult.failure("删除测评失败: " + e.getMessage());
        }
    }

    @Operation(
        summary = "获取测评分页列表", 
        description = "分页查询系统中的测评信息"
    )
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "获取成功",
                    content = @Content(schema = @Schema(implementation = ResponseResult.class))),
        @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    @GetMapping("/page")
    public ResponseResult<Map<String, Object>> getEvaluationPageList(
            @Parameter(name = "params", description = "分页查询参数，包含pageNum, pageSize等")
            @RequestParam Map<String, Object> params) {
        try {
            log.info("获取测评分页列表请求，查询参数: {}", params);
            ResponseResult<Map<String, Object>> result = evaluationService.getEvaluationPageList(params);
            
            if (result.isSuccess()) {
                log.info("获取测评分页列表成功");
            } else {
                log.warn("获取测评分页列表失败，原因: {}", result.getMsg());
            }
            
            return result;
        } catch (Exception e) {
            log.error("获取测评分页列表时发生异常: {}", e.getMessage(), e);
            return ResponseResult.failure("获取测评分页列表失败: " + e.getMessage());
        }
    }

    @Operation(
        summary = "获取用户参与的测评列表", 
        description = "查询当前用户参与的测评列表"
    )
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "获取成功",
                    content = @Content(schema = @Schema(implementation = ResponseResult.class))),
        @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    @GetMapping("/user")
    public ResponseResult<Map<String, Object>> getUserEvaluations() {
        try {
            log.info("获取用户参与的测评列表请求");
            ResponseResult<Map<String, Object>> result = evaluationService.getUserEvaluations();
            
            if (result.isSuccess()) {
                log.info("获取用户参与的测评列表成功");
            } else {
                log.warn("获取用户参与的测评列表失败，原因: {}", result.getMsg());
            }
            
            return result;
        } catch (Exception e) {
            log.error("获取用户参与的测评列表时发生异常: {}", e.getMessage(), e);
            return ResponseResult.failure("获取用户测评列表失败: " + e.getMessage());
        }
    }
}
